# -*- coding: utf-8 -*-
# coding=utf-8
'''
Created on 2014-3-20
用户数据从移动联盟导入
@author: SimonFu
'''
import os
import sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

import codecs

import DBCenter



'''
    导入category
'''

insert_user_sql = '''
    insert into `RosefinchEC`.`user_info`
                            (`user_id`,
                             `user_name`,
                             `real_name`,
                             `password`,
                             `salt`,
                             `email`,
                             `cellphone`,
                             `phone`,
                             `qq`,
                             `alipay_id`,
                             `alipay_name`,
                             `register_date`,
                             `register_ip`,
                             `last_login_date`,
                             `last_login_ip`,
                             `status`,
                             `create_user`,
                             `create_date`)
                values ('%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        %s,
                        '%s',
                        %s,
                        %s,
                        %s,
                        'UserImport',
                        NOW());
'''


def RunImport():
    select_all_user_sql = '''
        select * from zq_ucenter_members
    '''
    all_user_list = DBCenter.ydlm_db.query(select_all_user_sql)
    
    print len(all_user_list)
    
    select_all_userProfile_sql = '''
        select * from zq_common_member_profile
    '''
    all_userProfile_list = DBCenter.ydlm_db.query(select_all_userProfile_sql)
    print len(all_userProfile_list)
    
    num = 1
    for user_en in all_user_list:
        num = num + 1
        print num
        userProfile_list = [i for i in all_userProfile_list if(i.get('uid') == user_en.get('uid'))]
        # 若该用户在romlm不存在profile数据，则忽略此用户
        if(len(userProfile_list) == 0):
            continue
        
        userProfile_en = userProfile_list[0]
        
        # 若该用户已在EC数据库中存在，也忽略掉
        user_ec_count_sql = '''
            SELECT COUNT(1) AS `count` FROM user_info
            WHERE user_id = %s
        ''' % user_en.get('uid')
        user_ec_count = DBCenter.rc_db.get(user_ec_count_sql).get('count')
        if(user_ec_count > 0):
            continue

        insert_sql = insert_user_sql % (
                        user_en.get('uid'),
                        user_en.get('username'),
                        userProfile_en.get('realname'),
                        user_en.get('password'),
                        user_en.get('salt'),
                        user_en.get('email'),
                        userProfile_en.get('mobile'),
                        userProfile_en.get('telephone'),
                        userProfile_en.get('qq'),
                        userProfile_en.get('alipay'),
                        userProfile_en.get('aliname'),
                        'NULL',
                        user_en.get('regip'),
                        'NULL',
                        'NULL',
                        1
                    )
        try:
            DBCenter.rc_db.execute(insert_sql)
        except Exception, e:
            print str(e)
            print insert_sql
        
def Run():
    RunImport()
    
Run()
# print os.getcwd()
